// JavaScript Document
//定义数组地图
var Maps;
//定义当前关卡和接收地图内容
var GameIndex=0;
var MapMsg="";
//定义当前游戏地图
var Map;
//定义得到盒子元素
var GameMap;
//定义盒子宽高
var GameWidth;
var GameHeight;
//定义每个图片的宽高
var ImgWidth;
var ImgHeight;
//定义角色所在的位置
var HeroCoordinates=new Object();
//定义角色下一个所在位置
var NextCoordinates=new Object();
//角色所在位置的第三格位置
var nNextCoordinates=new Object();
//记录剑的数量
var count=0;
var sid;
var sid1;
var sid2;
//子弹位置
var bullet=new Object();
//定义怪物1,2
var MonsterCoordinates1=new Object();
var MonsterCoordinates2=new Object();
var MonsterCoordinates3=new Object();
//定义怪物下一格要走的位置
var NextMCoordinates1=new Object();
var NextMCoordinates2=new Object();
var NextMCoordinates3=new Object();
//角色生命值数
var lifes=10; 
//怪物1,2,3生命值数
var M1lifes=5;
var M2lifes=5;
var M3lifes=5;
//得到信息栏的div id
var FightText=document.getElementById("FightMsg");
window.onload=function Game(){
	initMaps();
	StartGame();
	}

//键盘控制移动
window.onkeydown=function keydown(ev){
	ev=ev||window.event;
	switch(ev.keyCode){
		//上
		case 38: 
		TopMove();
        break;
		//下
		case 40: 
		BottomMove();
        break;
		//左
		case 37:
		LeftMove();
        break;
		//右
		case 39:
		RightMove();
        break;
		case 88:
		shoot();
		break;
		}
	}
	//上下左右移动
	//上
function TopMove(){
	NextCoordinates.x=HeroCoordinates.x;
	NextCoordinates.y=HeroCoordinates.y-1;
	MoveTool(3);
}
	//下
function BottomMove(){
	NextCoordinates.x=HeroCoordinates.x;
	NextCoordinates.y=HeroCoordinates.y+1;
	MoveTool(2);
}
	//左
function LeftMove(){
	NextCoordinates.x=HeroCoordinates.x-1;
	NextCoordinates.y=HeroCoordinates.y;
	MoveTool(4);
	}
	//右
function RightMove(){
	NextCoordinates.x=HeroCoordinates.x+1;
	NextCoordinates.y=HeroCoordinates.y;
	MoveTool(5);
	}
	//人物移动工具
function MoveTool(msg){
	var Movevalue=Map[NextCoordinates.y][NextCoordinates.x];
	var hero=document.getElementById(HeroCoordinates.x+'-'+HeroCoordinates.y);
	var img=document.getElementById(NextCoordinates.x+'-'+NextCoordinates.y);
	//墙
if(Movevalue==1){
		//更新图片
		hero.src="images/1.png";
		img.src="images/"+msg+".png";
		//更新数组
		Map[HeroCoordinates.y][HeroCoordinates.x]=1;
		Map[NextCoordinates.y][NextCoordinates.x]=msg;
		
		HeroCoordinates.x=NextCoordinates.x;
		HeroCoordinates.y=NextCoordinates.y;
		}
		//碰到墙调整图片
if(Movevalue==0||Movevalue==14||Movevalue==17||Movevalue==18){
		hero.src="images/"+msg+".png";
		Map[HeroCoordinates.y][HeroCoordinates.x]=msg;
			}
		//剑
if(Movevalue==6){
		count++;	
		//更新图片
		hero.src="images/1.png";
		img.src="images/"+msg+".png";
		//更新数组
		Map[HeroCoordinates.y][HeroCoordinates.x]=1;
		Map[NextCoordinates.y][NextCoordinates.x]=msg;
		
		HeroCoordinates.x=NextCoordinates.x;
		HeroCoordinates.y=NextCoordinates.y;
if(count==2){
			alert("通关");
			GameIndex++;
			StartGame();
			}
		}
		
	}
//射击获取的值
function shoot(){

	//上
 if(Map[HeroCoordinates.y][HeroCoordinates.x]==3){
	NextCoordinates.x=HeroCoordinates.x;
	NextCoordinates.y=HeroCoordinates.y-1;
	/*nNextCoordinates.x=HeroCoordinates.x;
	nNextCoordinates.y=HeroCoordinates.y-2;*/
	 bulletMove(8);
 }
	//下
 if(Map[HeroCoordinates.y][HeroCoordinates.x]==2){
	NextCoordinates.x=HeroCoordinates.x;
	NextCoordinates.y=HeroCoordinates.y+1;
	/*nNextCoordinates.x=HeroCoordinates.x;
	nNextCoordinates.y=HeroCoordinates.y+2;*/
	bulletMove(9);
 }
	//左
	if(Map[HeroCoordinates.y][HeroCoordinates.x]==4){
	NextCoordinates.x=HeroCoordinates.x-1;
	NextCoordinates.y=HeroCoordinates.y;
	/*nNextCoordinates.x=HeroCoordinates.x-2;
	nNextCoordinates.y=HeroCoordinates.y;*/
	bulletMove(10);
 }
	//右
	if(Map[HeroCoordinates.y][HeroCoordinates.x]==5){
	NextCoordinates.x=HeroCoordinates.x+1;
	NextCoordinates.y=HeroCoordinates.y;
	/*nNextCoordinates.x=HeroCoordinates.x+2;
	nNextCoordinates.y=HeroCoordinates.y;*/
	bulletMove(11);
 }
}
//子弹射出
/*function shooting(){
	sid=setTimeout(bullet(),400);
}*/
//挥剑
function bulletMove(msg){
	var Movevalue=Map[NextCoordinates.y][NextCoordinates.x];
	var img=document.getElementById(NextCoordinates.x+'-'+NextCoordinates.y);
	//墙
if(Movevalue==1){
	img.src="images/"+msg+".png";
	Map[NextCoordinates.y][NextCoordinates.x]=msg;
	sid=setInterval("Time("+msg+")",50);
   }
   //怪物1
if(Movevalue==12||Movevalue==20){
	img.src="images/"+msg+".png";
	Map[NextCoordinates.y][NextCoordinates.x]=msg;
	FightMsg.innerHTML="";
	M1lifes--;
	FightMsg.innerHTML+="击中红龙！";
	FightMsg.innerHTML+="红龙当前血量："+M1lifes;
if(M1lifes==0){
	clearInterval(sid3);
	FightMsg.innerHTML="红龙死亡！";
	var M1=document.getElementById(MonsterCoordinates1.x+'-'+MonsterCoordinates1.y);
    M1.src="images/1.png";
	Map[MonsterCoordinates1.y][MonsterCoordinates1.x]=1;
	}
}
   //怪物2
if(Movevalue==13||Movevalue==19){
	img.src="images/"+msg+".png";
	Map[NextCoordinates.y][NextCoordinates.x]=msg;
	FightMsg.innerHTML="";
	M2lifes--;
	FightMsg.innerHTML+="击中骷髅兵！";
	FightMsg.innerHTML+="骷髅兵当前血量："+M2lifes;
if(M2lifes==0){
	clearInterval(sid4);
	FightMsg.innerHTML="骷髅兵死亡！";
	var M2=document.getElementById(MonsterCoordinates2.x+'-'+MonsterCoordinates2.y);
    M2.src="images/1.png";
	Map[MonsterCoordinates2.y][MonsterCoordinates2.x]=1;
	}
}
  //怪物3
if(Movevalue==21||Movevalue==22){
	img.src="images/"+msg+".png";
	Map[NextCoordinates.y][NextCoordinates.x]=msg;
	FightMsg.innerHTML="";
	M3lifes--;
	FightMsg.innerHTML+="击中幽灵！";
	FightMsg.innerHTML+="幽灵当前血量："+M3lifes;
if(M3lifes==0){
	clearInterval(sid5);
	FightMsg.innerHTML="幽灵死亡！";
	var M3=document.getElementById(MonsterCoordinates3.x+'-'+MonsterCoordinates3.y);
    M3.src="images/1.png";
	Map[MonsterCoordinates3.y][MonsterCoordinates3.x]=1;
	}
}
//箱子
if(Movevalue==17){
	img.src="images/"+msg+".png";
	Map[NextCoordinates.y][NextCoordinates.x]=msg;
	sid1=setInterval("Time("+msg+")",50);
	   }
//宝箱
if(Movevalue==18){
	img.src="images/"+msg+".png";
	Map[NextCoordinates.y][NextCoordinates.x]=msg;
	sid2=setInterval("Time2("+msg+")",50);
	   }
}
function Time(msg){
	bulletRes(msg);
}
function Time2(msg){
	boxRes(msg);
}
//挥剑还原地板
function bulletRes(msg){
	clearInterval(sid);
	clearInterval(sid1);
	var Movevalue=Map[NextCoordinates.y][NextCoordinates.x];
	var img=document.getElementById(NextCoordinates.x+'-'+NextCoordinates.y);
	if(Movevalue==msg){
	img.src="images/1.png";
	Map[NextCoordinates.y][NextCoordinates.x]=1;
	}
}
//挥剑掉落剑
function boxRes(msg){
	clearInterval(sid2);
	var Movevalue=Map[NextCoordinates.y][NextCoordinates.x];
	var img=document.getElementById(NextCoordinates.x+'-'+NextCoordinates.y);
	if(Movevalue==msg){
	img.src="images/6.png";
	Map[NextCoordinates.y][NextCoordinates.x]=6;
	Map[MonsterCoordinates1.y][MonsterCoordinates1.x]=1;
	}
}
//怪物移动,上下左右随机移动
var sid3=setInterval(function Monster1Msg(){
	var num=parseInt(Math.random()*4);
	//怪物1
if(num==0){
	NextMCoordinates1.x=MonsterCoordinates1.x;
	NextMCoordinates1.y=MonsterCoordinates1.y-1;
	Monster1Move(12);
	}
if(num==1){
	NextMCoordinates1.x=MonsterCoordinates1.x;
	NextMCoordinates1.y=MonsterCoordinates1.y+1;
	Monster1Move(12);
	}
if(num==2){
	NextMCoordinates1.x=MonsterCoordinates1.x+1;
	NextMCoordinates1.y=MonsterCoordinates1.y;
	Monster1Move(20);
	}
if(num==3){
	NextMCoordinates1.x=MonsterCoordinates1.x-1;
	NextMCoordinates1.y=MonsterCoordinates1.y;
	Monster1Move(12);
	}
},500);
var sid4=setInterval(function Monster2Msg(){
	//怪物2
	var num1=parseInt(Math.random()*4);
if(num1==0){
	NextMCoordinates2.x=MonsterCoordinates2.x;
	NextMCoordinates2.y=MonsterCoordinates2.y-1;
	Monster2Move(13);
	}
if(num1==1){
	NextMCoordinates2.x=MonsterCoordinates2.x;
	NextMCoordinates2.y=MonsterCoordinates2.y+1;
	Monster2Move(13);
	}
if(num1==2){
	NextMCoordinates2.x=MonsterCoordinates2.x+1;
	NextMCoordinates2.y=MonsterCoordinates2.y;
	Monster2Move(19);
	}
if(num1==3){
	NextMCoordinates2.x=MonsterCoordinates2.x-1;
	NextMCoordinates2.y=MonsterCoordinates2.y;
	Monster2Move(13);
	}
},500);
var sid5=setInterval(function Monster3Msg(){
	var num3=parseInt(Math.random()*4);
	//怪物3
	if(num3==0){
	NextMCoordinates3.x=MonsterCoordinates3.x;
	NextMCoordinates3.y=MonsterCoordinates3.y-1;
	Monster3Move(21);
	}
	if(num3==1){
	NextMCoordinates3.x=MonsterCoordinates3.x;
	NextMCoordinates3.y=MonsterCoordinates3.y+1;
	Monster3Move(21);
	}
	if(num3==2){
	NextMCoordinates3.x=MonsterCoordinates3.x+1;
	NextMCoordinates3.y=MonsterCoordinates3.y;
	Monster3Move(22);
	}
	if(num3==3){
	NextMCoordinates3.x=MonsterCoordinates3.x-1;
	NextMCoordinates3.y=MonsterCoordinates3.y;
	Monster3Move(21);
	}
},500);
//怪物1移动工具
function Monster1Move(num){
	var Movemsg=Map[NextMCoordinates1.y][NextMCoordinates1.x];
	var M1=document.getElementById(MonsterCoordinates1.x+'-'+MonsterCoordinates1.y);
	var img=document.getElementById(NextMCoordinates1.x+'-'+NextMCoordinates1.y);
	if(Movemsg==1){
		//更新图片
		M1.src="images/1.png";
		img.src="images/"+num+".png";
		//更新数组
		Map[MonsterCoordinates1.y][MonsterCoordinates1.x]=1;
		Map[NextMCoordinates1.y][NextMCoordinates1.x]=num;
		
		MonsterCoordinates1.x=NextMCoordinates1.x;
		MonsterCoordinates1.y=NextMCoordinates1.y;
		}
		if(Movemsg==2||Movemsg==3||Movemsg==4||Movemsg==5){
		   FightMsg.innerHTML="";
		  FightMsg.innerHTML+="怪物对你造成伤害,生命值-1<br />";
		  lifes--;
		  FightMsg.innerHTML+="当前生命值为："+lifes+"<br />";
		if(lifes==0){
		  FightMsg.innerHTML+="游戏结束！";
		  Game();
		}
		}
	}
//怪物2移动工具
function Monster2Move(num1){
	var Movemsg=Map[NextMCoordinates2.y][NextMCoordinates2.x];
	var M1=document.getElementById(MonsterCoordinates2.x+'-'+MonsterCoordinates2.y);
	var img=document.getElementById(NextMCoordinates2.x+'-'+NextMCoordinates2.y);
	if(Movemsg==1){
		//更新图片
		M1.src="images/1.png";
		img.src="images/"+num1+".png";
		//更新数组
		Map[MonsterCoordinates2.y][MonsterCoordinates2.x]=1;
		Map[NextMCoordinates2.y][NextMCoordinates2.x]=num1;
		
		MonsterCoordinates2.x=NextMCoordinates2.x;
		MonsterCoordinates2.y=NextMCoordinates2.y;
		}
		if(Movemsg==2||Movemsg==3||Movemsg==4||Movemsg==5){
		  FightMsg.innerHTML="";
		  FightMsg.innerHTML+="怪物对你造成伤害,生命值-1 <br />";
		  lifes--;
		  FightMsg.innerHTML+="当前生命值为："+lifes+"<br />";
		if(lifes==0){
		  FightMsg.innerHTML+="游戏结束！";
		  Game();
		}
		}
	}
//怪物3
function Monster3Move(num3){
	var Movemsg=Map[NextMCoordinates3.y][NextMCoordinates3.x];
	var M1=document.getElementById(MonsterCoordinates3.x+'-'+MonsterCoordinates3.y);
	var img=document.getElementById(NextMCoordinates3.x+'-'+NextMCoordinates3.y);
	if(Movemsg==1){
		//更新图片
		M1.src="images/1.png";
		img.src="images/"+num3+".png";
		//更新数组
		Map[MonsterCoordinates3.y][MonsterCoordinates3.x]=1;
		Map[NextMCoordinates3.y][NextMCoordinates3.x]=num3;
		
		MonsterCoordinates3.x=NextMCoordinates3.x;
		MonsterCoordinates3.y=NextMCoordinates3.y;
		}
		if(Movemsg==2||Movemsg==3||Movemsg==4||Movemsg==5){
		  FightMsg.innerHTML="";
		  FightMsg.innerHTML+="怪物对你造成伤害,生命值-1 <br />";
		  lifes--;
		  FightMsg.innerHTML+="当前生命值为："+lifes+"<br />";
		if(lifes==0){
		  FightMsg.innerHTML+="游戏结束！";
		  initMaps();
		  StartGame();
		}
		}
	}
//加载地图
function StartGame(){
	lifes=10;
	Map=Maps[GameIndex];
	MapMsg='';
	count=0;
	//初始化第一关
	for(var i=0;i<Map.length;i++){
		for(var j=0;j<Map[i].length;j++){
			MapMsg+="<img src='images/"+Map[i][j]+".png' height='"+ImgHeight+"px' width='"+ImgWidth+"px' id='"+j+"-"+i+"' />";
	 //记录英雄所在位置
	     if(Map[i][j]==2){
		  HeroCoordinates.x=j;
		  HeroCoordinates.y=i;
		 }
		 //记录怪物1位置
		if(Map[i][j]==12){
		  MonsterCoordinates1.x=j;
		  MonsterCoordinates1.y=i;
			}
		 //记录怪物2位置
		if(Map[i][j]==13){
		  MonsterCoordinates2.x=j;
		  MonsterCoordinates2.y=i;
			}
		if(Map[i][j]==21){
		  MonsterCoordinates3.x=j;
		  MonsterCoordinates3.y=i;
			}
		}
		}
		GameMap.innerHTML=MapMsg;
	}
//地图
function initMaps(){
	Maps=[
	       [
	        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
			[0, 1, 1, 1, 1, 1, 1, 17, 17, 1, 1, 1, 1, 1, 1, 0],
		    [0, 1, 1, 1, 1, 1, 1, 17, 17, 1, 1, 1, 1, 1, 1, 0],
			[0, 1, 1, 1, 1, 1, 1, 17, 17, 1, 1, 1, 1, 1, 1, 0],
			[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
			[0, 1, 1, 1, 1, 1, 1, 17, 1, 12, 1, 1, 1, 1, 1, 0],
			[0, 1, 14, 17, 14, 17, 14, 17, 17, 14, 17, 14, 17, 14, 18, 0],
			[0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 0],
			[0, 1, 1, 1, 1, 1, 1, 13, 1, 1, 1, 1, 1, 1, 1, 0],
			[0, 1, 14, 18, 14, 17, 14, 17, 17, 14, 17, 14, 17, 14, 17, 0],
			[0, 1, 1, 1, 1, 1, 1, 21, 1, 1, 1, 1, 1, 1, 1, 0],
			[0, 1, 14, 17, 14, 17, 14, 17, 17, 14, 17, 14, 17, 14, 17, 0],
			[0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
			[0, 1, 14, 17, 14, 17, 14, 17, 17, 14, 17, 14, 17, 14, 17, 0],
			[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
	       ],
	       [
	        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
			[0, 17, 14, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 14, 17, 0],
		    [0, 17, 1, 17, 1, 17, 14, 17, 17, 14, 17, 1, 17, 1, 17, 0],
			[0, 17, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 17, 0],
			[0, 17, 14, 17, 1, 17, 14, 17, 17, 14, 17, 1, 17, 14, 17, 0],
			[0, 0, 0, 1, 1, 0, 1, 1, 12, 1, 0, 1, 1, 0, 0, 0],
			[0, 17, 14, 17, 1, 17, 14, 18, 17, 14, 17, 1, 17, 14, 17, 0],
			[0, 1, 1, 1, 1, 1, 13, 0,  0, 1, 1, 1, 1, 1, 1, 0],
			[0, 2, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0],
			[0, 17, 14, 17, 1, 17, 14, 18, 17, 14, 17, 1, 17, 14, 17, 0],
			[0, 0, 0, 1, 1, 0, 1, 21, 1, 1, 0, 1, 1, 0, 0, 0],
			[0, 17, 14, 17, 1, 17, 14, 17, 17, 14, 17, 1, 17, 14, 17, 0],
			[0, 17, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 17, 0],
			[0, 17, 1, 17, 1, 17, 14, 17, 17, 14, 17, 1, 17, 1, 17, 0],
			[0, 17, 14, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 14, 17, 0],
			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
	       ],
		   
		   [
	        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
			[0, 17, 14, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 14, 17, 0],
		    [0, 17, 1, 17, 1, 17, 14, 17, 17, 14, 17, 1, 17, 1, 17, 0],
			[0, 17, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 17, 0],
			[0, 17, 14, 17, 1, 17, 14, 17, 17, 14, 17, 1, 17, 14, 17, 0],
			[0, 0, 0, 1, 1, 0, 1, 1, 12, 1, 0, 1, 1, 0, 0, 0],
			[0, 17, 14, 17, 1, 17, 14, 18, 17, 14, 17, 1, 17, 14, 17, 0],
			[0, 1, 1, 1, 1, 1, 13, 0,  0, 1, 1, 1, 1, 1, 1, 0],
			[0, 2, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0],
			[0, 17, 14, 17, 1, 17, 14, 18, 17, 14, 17, 1, 17, 14, 17, 0],
			[0, 0, 0, 1, 1, 0, 1, 21, 1, 1, 0, 1, 1, 0, 0, 0],
			[0, 17, 14, 17, 1, 17, 14, 17, 17, 14, 17, 1, 17, 14, 17, 0],
			[0, 17, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 17, 0],
			[0, 17, 1, 17, 1, 17, 14, 17, 17, 14, 17, 1, 17, 1, 17, 0],
			[0, 17, 14, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 14, 17, 0],
			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
	       ],
		 ];
		 //得到地图div
		 GameMap=document.getElementById("GameMap");
		 //得到div宽高
		 GameWidth=parseInt(GameMap.style.width);
		 GameHeight=parseInt(GameMap.style.height);
		 //初始化每个图片的大小
		 ImgWidth=GameWidth/Maps[0][0].length;
		 ImgHeight=GameHeight/Maps[0][0].length;
}